package com.bw.house.page

import androidx.activity.viewModels
import androidx.appcompat.widget.SearchView
import com.alibaba.android.arouter.facade.annotation.Route
import com.bw.base.path.RouterPath
import com.bw.common.view.BaseMvvmActivity
import com.bw.house.R
import com.bw.house.adapter.TenantAdapter
import com.bw.house.databinding.ActivityTenantBinding
import com.bw.house.vm.HouseViewModel
import dagger.hilt.android.AndroidEntryPoint
@Route(path = RouterPath.PATH_TENANT_PATH)
@AndroidEntryPoint
class TenantActivity : BaseMvvmActivity<ActivityTenantBinding>() {
    override fun getLayoutId(): Int = R.layout.activity_tenant
    var keyword = ""

    private val mViewModel:HouseViewModel by viewModels()
    val adapter = TenantAdapter()

    override fun initViews() {
        super.initViews()
        mDataBinding.tenantRv.adapter =adapter
        mViewModel.mTenantSuccess.observe(this){
            if (it.first().rented== true){
                adapter.data.addAll( it)
            }
            adapter.notifyDataSetChanged()
        }

        //搜索
        mDataBinding.tenantSearch.setOnQueryTextListener(object :android.widget.SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                if (query!=null){
                    mViewModel.mTenantSuccess.observe(this@TenantActivity){
                        adapter.data.clear()
                        adapter.data.addAll(it.filter { it.rented==true&&it.enterpriseName.contains(query) })
                        adapter.notifyDataSetChanged()
                    }
                }
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                mViewModel.mTenantSuccess.observe(this@TenantActivity){
                    adapter.data.clear()
                    adapter.data.addAll(it.filter { it.rented==true })
                    adapter.notifyDataSetChanged()
                }
                return true
            }

        })


    }

    override fun loadData() {
        super.loadData()
        mViewModel.getTenantData()


    }



}